//
//  NovelDataBase.m
//  UI-20-数据库
//
//  Created by dllo on 15/8/14.
//  Copyright (c) 2015年 蓝欧科技. All rights reserved.
//

#import "NovelDataBase.h"
#import "DB.h"
#import "StrategyModel.h"
#import "ShopModel.h"
@implementation NovelDataBase

+ (void)insertWithModel:(StrategyModel *)model
{
    //第一步:获得数据库指针
    sqlite3 *db = [DB dbOpen];
    //第二步:获得数据库语句
    NSString *str = [NSString stringWithFormat:@"insert into a_table(a_id, a_title, a_likescount, a_image, a_url) values('%@', '%@', '%@', '%@', '%@')", model.Id,  model.title, model.likes_count , model.cover_image_url, model.url];
    //第三步:执行sql语句
    int result = sqlite3_exec(db, [str UTF8String], nil, nil, nil);
    if (result == SQLITE_OK) {
        NSLog(@"执行成功");
    } else {
        NSLog(@"执行失败");
    }
}



+ (void)twoinsertWithModel:(ShopModel *)model
{
    //第一步:获得数据库指针
    sqlite3 *db = [DB dbOpen];
    //第二步:获得数据库语句
    NSString *str = [NSString stringWithFormat:@"insert into b_table(b_id, b_name, b_favorites_count, b_image, b_price, b_url) values('%@', '%@', '%ld', '%@', '%@', '%@')", model.Id,  model.name, (long)model.favorites_count , model.cover_image_url, model.price, model.url];
    //第三步:执行sql语句
    int result = sqlite3_exec(db, [str UTF8String], nil, nil, nil);
    if (result == SQLITE_OK) {
        NSLog(@"执行成功");
    } else {
        NSLog(@"执行失败");
    }
}


//删除攻略
+ (void)deleteWithModle:(StrategyModel *)modle
{
    sqlite3 *db = [DB dbOpen];
    NSString *str = [NSString stringWithFormat:@"delete from a_table where a_id = %@",modle];
    int result = sqlite3_exec(db, [str UTF8String], nil, nil, nil);
    if (result == SQLITE_OK) {
        NSLog(@"删除成功");
    } else {
        NSLog(@"删除失败");
    }
}

//删除攻略
+ (void)deleteWithMyId:(NSString *)MyId
{
    sqlite3 *db = [DB dbOpen];
    NSString *str = [NSString stringWithFormat:@"delete from a_table where a_id = %@", MyId];
    int result = sqlite3_exec(db, [str UTF8String], nil, nil, nil);
    if (result == SQLITE_OK) {
        NSLog(@"删除成功");
    } else {
        NSLog(@"删除失败");
    }
}
//删除商品
+ (void)twodeleteWithMyId:(NSString *)MyId
{
    sqlite3 *db = [DB dbOpen];
    NSString *str = [NSString stringWithFormat:@"delete from b_table where b_id = %@",MyId];
    int result = sqlite3_exec(db, [str UTF8String], nil, nil, nil);
    if (result == SQLITE_OK) {
        NSLog(@"删除成功");
    } else {
        NSLog(@"删除失败");
    }
}

//查询:1. 查询所有的 
+ (NSArray *)selectedAllMyId
{
    //获得数据库指针
     sqlite3 *db = [DB dbOpen];
    //定义数据库替身
    sqlite3_stmt *stmt = nil;
    NSString *str = [NSString stringWithFormat:@"select a_id, a_title, a_likescount, a_image ,a_url from a_table "];
    //'执行
    int result = sqlite3_prepare_v2(db, [str UTF8String], -1, &stmt, nil);
    NSMutableArray *array = [NSMutableArray array];
    if (result == SQLITE_OK) {
        //判断是否有下一行数据
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            
            const unsigned char *a_id = sqlite3_column_text(stmt, 0);
            const unsigned char *a_title = sqlite3_column_text(stmt, 1);
            const unsigned char *a_likescount = sqlite3_column_text(stmt, 2);
            const unsigned char *a_image = sqlite3_column_text(stmt, 3);
            const unsigned char *a_url = sqlite3_column_text(stmt, 4);
            
            NSString *aa_id = [NSString stringWithUTF8String:(const char *)a_id];
            NSString *aa_title = [NSString stringWithUTF8String:(const char *)a_title];
            NSString *aa_likescount = [NSString stringWithUTF8String:(const char *)a_likescount];
            NSString *aa_image = [NSString stringWithUTF8String:(const char *)a_image];
            NSString *aa_url = [NSString stringWithUTF8String:(const char *)a_url];
            
            StrategyModel *model = [[[StrategyModel alloc] init] autorelease];
            model.Id = aa_id;
            model.title= aa_title;
            model.likes_count = aa_likescount;
            model.cover_image_url = aa_image;
            model.url = aa_url;
            
            [array addObject:model];
        }
    }
    //释放替身
    sqlite3_finalize(stmt);
    return array;
}


//查询:1. 查询所有的
+ (NSArray *)twoselectedAllMyId
{
    //获得数据库指针
    sqlite3 *db = [DB dbOpen];
    //定义数据库替身
    sqlite3_stmt *stmt = nil;
    NSString *str = [NSString stringWithFormat:@"select b_id, b_name, b_favorites_count, b_image, b_price , b_url from b_table"];
    //'执行
    int result = sqlite3_prepare_v2(db, [str UTF8String], -1, &stmt, nil);
    NSMutableArray *array = [NSMutableArray array];
    if (result == SQLITE_OK) {
        //判断是否有下一行数据
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            
            const unsigned char *b_id = sqlite3_column_text(stmt, 0);
            const unsigned char *b_name = sqlite3_column_text(stmt, 1);
            NSInteger  b_favorites_count = sqlite3_column_int(stmt, 2);
            const unsigned char *b_image = sqlite3_column_text(stmt, 3);
            const unsigned char *b_price = sqlite3_column_text(stmt, 4);
            const unsigned char *b_url = sqlite3_column_text(stmt, 5);
            
            NSString *bb_id = [NSString stringWithUTF8String:(const char *)b_id];
            NSString *bb_name = [NSString stringWithUTF8String:(const char *)b_name];
            NSString *bb_image = [NSString stringWithUTF8String:(const char *)b_image];
            NSString *bb_price = [NSString stringWithUTF8String:(const char *)b_price];
            NSString *bb_url = [NSString stringWithUTF8String:(const char *)b_url];
            
            ShopModel *model = [[[ShopModel alloc] init] autorelease];
            model.Id = bb_id;
            model.name= bb_name;
            model.favorites_count = b_favorites_count;
            model.cover_image_url = bb_image;
            model.price = bb_price;
            model.url = bb_url;
            
            [array addObject:model];
        }
    }
    //释放替身
    sqlite3_finalize(stmt);
    return array;
}

//查询:1. 查询单个的
+ (BOOL)selectedMyId:(NSString *)MyId
{
    //获得数据库指针
    sqlite3 *db = [DB dbOpen];
    //定义数据库替身
    sqlite3_stmt *stmt = nil;
    NSString *str = [NSString stringWithFormat:@"select a_id, a_title, a_likescount, a_image ,a_url from a_table "];
    //'执行
    int result = sqlite3_prepare_v2(db, [str UTF8String], -1, &stmt, nil);
    if (result == SQLITE_OK) {
        //判断是否有下一行数据
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            
            const unsigned char *a_id = sqlite3_column_text(stmt, 0);
           
            
            NSString *aa_id = [NSString stringWithUTF8String:(const char *)a_id];
            NSString *str = [NSString stringWithFormat:@"%@", MyId];
            if ([aa_id isEqualToString:str]) {
                return YES;
            }
        }
    }
    else {
        return NO;
    }
    //释放替身
    sqlite3_finalize(stmt);
    return YES;
}
//查询:2. 查询单个的
+ (BOOL)twoselectedMyId:(NSString *)MyId
{
    //获得数据库指针
    sqlite3 *db = [DB dbOpen];
    //定义数据库替身
    sqlite3_stmt *stmt = nil;
    NSString *str = [NSString stringWithFormat:@"select b_id, b_name, b_favorites_count, b_image, b_price , b_url from b_table"];
    //'执行
    int result = sqlite3_prepare_v2(db, [str UTF8String], -1, &stmt, nil);
    if (result == SQLITE_OK) {
        //判断是否有下一行数据
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            
            const unsigned char *a_id = sqlite3_column_text(stmt, 0);
            
            
            NSString *aa_id = [NSString stringWithUTF8String:(const char *)a_id];
            
            if ([aa_id isEqualToString:MyId]) {
                return YES;
            }
        }
    }
    else {
        return NO;
    }
    //释放替身
    sqlite3_finalize(stmt);
    return YES;
}

+ (StrategyModel *)selectedAllWithA_id:(NSInteger )myId
{
    //获得数据库指针
    sqlite3 *db = [DB dbOpen];
    //定义数据库替身
    sqlite3_stmt *stmt = nil;
    NSString *str = [NSString stringWithFormat:@"select a_id, a_title from t_table where a_id = %ld", (long)myId];
    //'执行
    int result = sqlite3_prepare_v2(db, [str UTF8String], -1, &stmt, nil);
    if (result == SQLITE_OK) {
        //判断是否有下一行数据
        if (sqlite3_step(stmt) == SQLITE_ROW) {
           const unsigned char *a_id = sqlite3_column_text(stmt, 0);
           NSString *aa_id = [NSString stringWithUTF8String:(const char *)a_id];
           StrategyModel *model = [[[StrategyModel alloc] init] autorelease];
            model.Id = aa_id;
          
            sqlite3_finalize(stmt);
            return model;
        }
    }
    //释放替身
    sqlite3_finalize(stmt);
    return nil;
}
@end
